package controller;

import java.sql.SQLException;
import java.util.List;

import org.codehaus.jettison.json.JSONException;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import conpool.PoolManager;
import remote.ConfigService;
import service.CollectionData;

@RestController
public class Sqlcontroller {
	
	@Autowired
	CollectionData collectionData;
	
	@Autowired
	ConfigService configService;

    @Value("${management.security.enabled}")
    boolean management;
    
    @Value("${config}")
	String config;
	
	@RequestMapping(value="/baseSearch")
	public String baseSearch(@RequestParam String table,@RequestParam int num,@RequestParam String sourceName){
		List<JSONObject> datalist = null;
		try {
			try {
				datalist = collectionData.dataToJson(table, num, PoolManager.getInstance().getPoolByName(sourceName));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		System.out.println("查询结果："+JSON.toJSON(datalist).toString());
		return JSON.toJSON(datalist).toString();
	}
	
	@RequestMapping(value="/getconfig")
	public String getconfig(@RequestParam String sourceType){
		return configService.getConfig(sourceType);
	}
	
	@RequestMapping(value="/getinitconfig")
	public boolean getinitconfig(){
		return management;
	}
}
